// fetchImage.mjs

import fetch from 'node-fetch';
import fs from 'fs';


async function fetchAndSaveImage() {
  try {
    const response = await fetch("https://4kwallpapers.com/images/walls/thumbs_2t/11770.png", {
      "headers": {
        "accept": "image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8",
        "accept-language": "zh-CN,zh;q=0.9",
        "sec-ch-ua": "\"Chromium\";v=\"119\", \"Not?A_Brand\";v=\"24\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-dest": "image",
        "sec-fetch-mode": "no-cors",
        "sec-fetch-site": "same-origin",
        "cookie": "_gid=GA1.2.524899745.1720240401; __gads=ID=225117520a575cf3:T=1720240401:RT=1720240401:S=ALNI_MZ8WmHzlTO02wj2-vHi1TMzMMfztw; __gpi=UID=00000e754ff42831:T=1720240401:RT=1720240401:S=ALNI_MZrIFW1LngfXwgyd1Qv7kW3XIS-4g; __eoi=ID=b60a9d24e9838c3b:T=1720240401:RT=1720240401:S=AA-Afjbtu1HL_vGhmNUgO8BYNSsM; _ga=GA1.1.698118757.1720240401; FCNEC=%5B%5B%22AKsRol8zVPScsq_HdeGPVYj_CjNMh-8YOedqc22HOw54mQ1IpaDihbkptVgof_bI_JcIDE-5hJhUT99dpVqSfhT8HTjk26w7tsPoLPtz49UT-CL2KdG6dKQ1twhXBB-gKStoUfyxD6UaIjyGNZelrqYFJZ5vmVsa4w%3D%3D%22%5D%5D; _ga_TXM0B6HHQC=GS1.1.1720240400.1.1.1720241097.60.0.0",
        "Referer": "https://4kwallpapers.com/abstract/layers-3d-17326.html",
        "Referrer-Policy": "strict-origin-when-cross-origin"
      },
      "body": null,
      "method": "GET"
    });

    if (!response.ok) {
      throw new Error(`HTTP error! Status: ${response.status}`);
    }

    const arrayBuffer = await response.arrayBuffer();
    const buffer = Buffer.from(arrayBuffer);
    fs.writeFileSync('./11770.jpg', buffer);
    console.log('图片保存成功。');
  } catch (error) {
    console.error('获取和保存图片时出错:', error.message);
  }
}


fetchAndSaveImage();
